package com.recursivechaos.calendr;

import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.List;

import com.google.gdata.client.calendar.CalendarQuery;
import com.google.gdata.client.calendar.CalendarService;
import com.google.gdata.data.calendar.CalendarEventEntry;
import com.google.gdata.data.calendar.CalendarEventFeed;

public class CalendarWorker {

        private String calendar;

        public CalendarWorker(String stringSetting) {
                this.calendar = stringSetting;
        }

        public String[] getMeetupTicker() throws Exception {

                String[] meetupList = new String[6];
                // Set up the URL and the object that will handle the connection:
                // Update this to reflect your Domain-App-#
                CalendarService myService = new CalendarService(
                                "RecursiveChaos-IRC_GAMEMASTER-1");
                URL feedUrl = new URL(calendar);
                CalendarQuery query = new CalendarQuery(feedUrl);
                // Resolve Recurring Events to single events
                query.setStringCustomParameter("singleevents", "true");
                // Order by start Time ascending
                query.setStringCustomParameter("orderby", "starttime");
                query.setStringCustomParameter("sortorder", "ascending");
                // Events in the future
                query.setStringCustomParameter("futureevents", "true");
                // Start the Query for CalendarEvents
                CalendarEventFeed resultFeed;
                resultFeed = myService.query(query, CalendarEventFeed.class);
                // Create list of calendar events
                List<CalendarEventEntry> entries = resultFeed.getEntries();
                // Sets max replies to 5
                int max = 5;
                if (max > entries.size()) {
                        max = entries.size();
                }

                // adds String text of next event
                for (int i = 0; i < max; i++) {
                        CalendarEventEntry entry = entries.get(i);
                        String title = entry.getTitle().getPlainText();
                        String location = "";
                        String outString;
                        long starttime = entry.getTimes().get(0).getStartTime().getValue();
                        long endtime = entry.getTimes().get(0).getEndTime().getValue();
                        java.util.Date eventDate;

                        long SEC_IN_DAY = 86400000;
                        if ((endtime - starttime) == SEC_IN_DAY) {
                                eventDate = new java.util.Date(starttime + SEC_IN_DAY);
                                SimpleDateFormat format = new SimpleDateFormat("MM/dd");
                                outString = (title + " - " + format.format(eventDate));
                        } else {
                                eventDate = new java.util.Date(starttime);
                                SimpleDateFormat format = new SimpleDateFormat("MM/dd h:mm a");
                                outString = (title + " - " + format.format(eventDate));
                        }
                        // get location, if available
                        if (entry.getLocations().size() > 0) {
                                location = entry.getLocations().get(0).getValueString();
                        }
                        if (!location.equals("")) {
                                outString = outString + " at " + location;
                        }
                        meetupList[i] = outString;
                }
                return meetupList;
        }

        public void setCalendar(String calendar) {
                this.calendar = calendar;

        }

}